<?php
declare (strict_types=1);

namespace app\admin\validate;

use app\common\model\User as UserModel;
use think\Validate;

class User extends Validate
{
    protected $rule = [
        'nickname' => 'require',
        'mobile' => 'require|mobile|checkOnly:mobile'
    ];

    protected $message = [
        'nickname.require' => '请输入昵称',
        'mobile.require' => '请输入手机号',
        'mobile.mobile' => '手机号格式错误',
        'mobile.checkOnly' => '手机号已存在'
    ];

    protected function checkOnly($value, $rule, $data = []): bool
    {
        $that = UserModel::where([$rule => $value, 'is_del' => 0]);
        if (!empty($data['id'])) {
            $that->where('id', '<>', $data['id']);
        }
        $count = $that->count();

        return $count ? false : true;
    }
}